package ec.atreasure.com.core.app;

import android.app.Activity;
import android.os.Handler;

import com.joanzapata.iconify.IconFontDescriptor;
import com.joanzapata.iconify.Iconify;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;

import okhttp3.Interceptor;

/**
 * Description : 配置文件存储及获取（静态内部类线程安全单例）
 * Created by wangjin on 2019/6/21.
 * Job number：
 * Phone ：18301070822
 * Email： 120182051@qq.com
 * Person in charge : 汪渝栋
 * Leader：
 */
public class Configurator {

    //配置项缓存
    private static final HashMap<Object, Object> ATREASURE_CONFIGS = new HashMap<>();

    //图标存储
    private static List<IconFontDescriptor> ICONS = new ArrayList<>();

    //网络拦截器缓存
    private static ArrayList<Interceptor> INTERCEPTORS = new ArrayList<>();

    //全局Handler
    private static final Handler HANDLER = new Handler();

    public Configurator() {
        ATREASURE_CONFIGS.put(ConfigType.CONFIG_READY.name(), false);
        ATREASURE_CONFIGS.put(ConfigType.HANDLER, HANDLER);
    }

    private static class ConfiguratorHolder {
        static final Configurator INSTANCE = new Configurator();
    }

    public static Configurator getInstance() {
        return ConfiguratorHolder.INSTANCE;
    }

    /**
     * 配置完毕
     */
    public final void configure() {
        initIcons();
        ATREASURE_CONFIGS.put(ConfigType.CONFIG_READY.name(), true);
    }

    /**
     * 获取配置信息缓存
     *
     * @return
     */
    public final HashMap<Object, Object> getAtreasureConfigs() {
        return ATREASURE_CONFIGS;
    }

    /**
     * 配置host
     *
     * @param host
     * @return
     */
    public final Configurator withApiHost(String host) {
        ATREASURE_CONFIGS.put(ConfigType.API_HOST, host);
        return this;
    }


    /**
     * 配置网络拦截器
     *
     * @param interceptor
     * @return
     */
    public final Configurator withInterceptor(Interceptor interceptor) {
        INTERCEPTORS.add(interceptor);
        ATREASURE_CONFIGS.put(ConfigType.INTERCEPTOR, INTERCEPTORS);
        return this;
    }

    /**
     * 配置网络拦截器
     *
     * @param interceptors
     * @return
     */
    public final Configurator withInterceptor(List<Interceptor> interceptors) {
        INTERCEPTORS.addAll(interceptors);
        ATREASURE_CONFIGS.put(ConfigType.INTERCEPTOR, INTERCEPTORS);
        return this;
    }

    /**
     * 配置微信appid
     * @param wxAppId
     * @return
     */
    public final Configurator withWXAppId(String wxAppId) {
        ATREASURE_CONFIGS.put(ConfigType.WE_CHAT_APP_ID, wxAppId);
        return this;
    }

    /**
     * 配置微信secret
     * @param secret
     * @return
     */
    public final Configurator withWXSecret(String secret) {
        ATREASURE_CONFIGS.put(ConfigType.WE_CHAT_APP_SECRET, secret);
        return this;
    }

    /**
     * 配置全局activity
     * @param activity
     * @return
     */
    public final Configurator withActivity(Activity activity) {
        ATREASURE_CONFIGS.put(ConfigType.ACTIVITY, activity);
        return this;
    }

    /**
     * 校验配置是否完成
     */
    public static final void checkConfigIsReady() {
        final boolean isReady = (boolean) ATREASURE_CONFIGS.get(ConfigType.CONFIG_READY.name());
        if (!isReady) {
            throw new RuntimeException("配置未完成!!!");
        }
    }

    /**
     * 根据key值获取配置信息
     *
     * @param key
     * @param <T>
     * @return
     */
    final <T> T getConfiguretion(Object key) {
        checkConfigIsReady();
        final Object value = ATREASURE_CONFIGS.get(key);
        if (value == null) {
            throw new NullPointerException(key.toString() + " IS NULL");
        }
        return (T) ATREASURE_CONFIGS.get(key);
    }

    /**
     * 初始化图标配置
     */
    public static void initIcons() {
        if (ICONS.size() > 0) {
            Iconify.IconifyInitializer iconifyInitializer = Iconify.with(ICONS.get(0));
            for (int i = 1; i < ICONS.size(); i++) {
                iconifyInitializer.with(ICONS.get(i));
            }
        }
    }

    /**
     * 加入图标
     *
     * @param descriptor
     */
    public final Configurator withIcon(IconFontDescriptor descriptor) {
        ICONS.add(descriptor);
        return this;
    }
}


